package com.honglang.module.album.action;

import java.io.File;

import com.honglang.common.tools.Constants;
import com.honglang.module.album.entity.Photo;
import com.honglang.module.album.service.AlbumPhotoService;
import com.honglang.module.album.service.PhotoService;
import com.honglang.service.FileSysService;
import com.honglang.struts2.action.BaseAction;
import com.opensymphony.xwork2.Action;

public class PhotoManagerAction extends BaseAction{
	private PhotoService photoService;
	private FileSysService fileSysService;
	private AlbumPhotoService albumPhotoService;
	
	
	/**
	 * get one pager of photo
	 * @return
	 */
	public String prePhotoManager(){
		String pageNoStr = this.getRequest().getParameter("pageNo");
		int pageNo = Constants.DEFAULT_PAGE_NO;
		if(null != pageNoStr){
			pageNo = Integer.valueOf(this.getRequest().getParameter("pageNo"));
		}
		this.getRequest().setAttribute("pager", this.photoService.getPager(Constants.PHOTO_PAGE_SIZE, pageNo));
		return Action.SUCCESS;
	}
	
	
	
	
	
	
	/**
	 *  delete photo file
	 * @return
	 */
	public String deletePhoto(){
		String photoId = this.getRequest().getParameter("pId");
		if(null != photoId){
			Photo photo = this.getPhotoService().getPhoto(Integer.valueOf(photoId));
			this.getAlbumPhotoService().delete(this.getAlbumPhotoService().getAlbumPhotoByPhotoId(photo.getId()));
			this.getFileSysService().deleteByRelativeUrl(this.getFileSysService().getFileSysByRelativeUrl(photo.getUrl()));
			new File(photo.getAbsoluteUrl()).delete();
			this.getPhotoService().delete(photo);
			
		}
		return Action.SUCCESS;
	}
	
	
	
	
	
	
	
	

	public PhotoService getPhotoService() {
		return photoService;
	}

	public void setPhotoService(PhotoService photoService) {
		this.photoService = photoService;
	}


	public FileSysService getFileSysService() {
		return fileSysService;
	}

	public void setFileSysService(FileSysService fileSysService) {
		this.fileSysService = fileSysService;
	}


	public AlbumPhotoService getAlbumPhotoService() {
		return albumPhotoService;
	}


	public void setAlbumPhotoService(AlbumPhotoService albumPhotoService) {
		this.albumPhotoService = albumPhotoService;
	}
	
	
}
